package com.itheima.config;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
 * @author huyoufu <https://github.com/huyoufu>
 * @TIME 2020-08-06 14:39
 * @description
 */
@PropertySource("classpath:redis.properties")
public class RedisConfig {
    @Value("${redis.host}")
    private String host;
    @Value("${redis.port}")
    private int port;

    @Value("${redis.maxIdle}")
    private int maxIdle;
    @Value("${redis.maxTotal}")
    private int maxTotal;
    @Value("${redis.minIdle}")
    private int minIdle;
    @Value("${redis.maxWaitMillis}")
    private int maxWaitMillis;

    @Bean
    public GenericObjectPoolConfig createRedisConfig(){
        GenericObjectPoolConfig config=new JedisPoolConfig();

        config.setMaxIdle(maxIdle);
        config.setMaxTotal(maxTotal);
        config.setMinIdle(minIdle);
        config.setMaxWaitMillis(maxWaitMillis);
        return config;
    }

    /**
     * 如果你需要用另外一个在容器放着的对象 只要把写到参数就可以了GenericObjectPoolConfig
     */
    @Bean
    public JedisPool createJedisPool(GenericObjectPoolConfig config){

        //创建这个jedis连接池代码
        JedisPool pool = new JedisPool(config,host,port);

        return pool;

    }
}
